% Find neighborhood neurons to a given one according to rad
% it assumes a mxn (m row, n cols) rectangle as the network topology
% dncampo
%-------------------------------------------------------------------------

function nei = neighborhood(neu, rad, top)
    nei = [];
    
	 a = vertical_neighborhood (neu, rad, top);
	 nei = [nei a];
	 for i = 1:rad
		tmp = neu - i*top(1);
	   a = vertical_neighborhood (tmp, rad-i, top);
		nei = [nei a];
		
		tmp = neu + i*top(1);
		a = vertical_neighborhood (tmp, rad-i, top);
		nei = [nei a];
	 end

function neurons = vertical_neighborhood (neu, rad, top)
  neurons = [];
  if (in_map (neu, top))
	 sup_edge = mod(neu - 1, top(1));
	 inf_edge = top(1) - (mod(neu - 1, top(1))) - 1;
  
	 neurons = [neurons (neu-min(sup_edge, rad)):neu];
	 neurons = [neurons neu+1:(neu+min(inf_edge, rad))];
  end
  
  

function bool = in_map (neu, top)
  bool = neu > 0 && neu <= (top(1)*top(2));



function funcion_anterior
	 %neu = neu - rad*top(1);
    
    tmp = neu - rad*top(1);
    size = 0;
    for i = 0:1:rad
        if (tmp > 0)
            nei = [nei tmp:tmp+size];
        end
        size = size + 2;
        tmp = tmp + top(1) - 1;
    end
    
    tmp = tmp+2;
    size = size - 2;
    
    for i = rad-1:-1:0
        size = size - 2;
        nei = [nei tmp:tmp+size];
        tmp = tmp + top(1) + 1;
    end    
    

	 


